<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>

        <el-col :span="24">
          <el-form-item :label="$t('流程标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('流程标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('报告名称')" prop="reportName">
            <el-input v-model="form.reportName" :placeholder="$t('报告名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('报告类型')">
            <dictSelect
              v-model="form.reportType"
              :clearable="true"
              dictionary="deal_after_report_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('重大事项说明')" prop="description">
            <el-input v-model="form.description" type="textarea" :placeholder="$t('重大事项说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('重大风险分析')" prop="description">
            <el-input v-model="form.riskAnalysis" type="textarea" :placeholder="$t('重大风险分析')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('其他说明')" prop="otherDescription">
            <el-input v-model="form.otherDescription" type="textarea" :placeholder="$t('其他说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--文件-->
      <FileTable :list.sync="form.fileList" has-permi="deal:afterReport" data-type="deal_after_report" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="deal:afterReport" :menu-key="menuKey" :btn-hide="btnHide" />
    </div>
  </div>
</template>

<script>
import { saveDealAfterReport, getDealAfterReport } from '@/api/deal/afterReport/dealAfterReport'
export default {
  data() {
    return {
      // 菜单标识
      menuKey: 'deal_after_report',
      otherBtnHide: false,
      // 是否隐藏保存按钮
      btnHide: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actTitle: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], reportName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], reportType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealAfterReport(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        actId: undefined,
        actTitle: undefined,
        actStatus: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        dealId: undefined,
        companyId: this.$route.query.companyId,
        reportName: undefined,
        reportType: undefined,
        description: undefined,
        riskAnalysis: undefined,
        otherDescription: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveDealAfterReport(this.form).then(response => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 启动流程
                  this.startProcess(response.data)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                  resolve()
                }
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.form.actTitle
      // 业务表名
      processData.dataTable = 'deal_after_report'
      // 业务ID
      processData.dataId = dataId.id
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/deal/afterReport/dealAfterReportForm'
      processData.viewPath = '@/views/deal/afterReport/dealAfterReportView'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.menuKey
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }

}
</script>
